#include "RGBLED.h"
// --------------------
Adafruit_NeoPixel LR_Strip = Adafruit_NeoPixel(RL_NUMS, RL_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel FB_Strip = Adafruit_NeoPixel(FB_NUMS, FB_PIN, NEO_GRB + NEO_KHZ800);

// --------------------
void RGBLED ::Initialize()
{
    FB_Strip.begin();
    LR_Strip.begin();
    FB_Strip.setBrightness(50); // 设置亮度
};

void RGBLED::extractRGB(const String &colorStr, uint8_t &r, uint8_t &g, uint8_t &b)
{
    // 移除开头的 '{' 和结尾的 '}'
    int startIndex = colorStr.indexOf('{') + 1;
    int endIndex = colorStr.lastIndexOf('}');
    String colorData = colorStr.substring(startIndex, endIndex);

    // 分割字符串并转换为整数
    int commaIndex1 = colorData.indexOf(',');
    int commaIndex2 = colorData.indexOf(',', commaIndex1 + 1);
    r = colorData.substring(0, commaIndex1).toInt();
    g = colorData.substring(commaIndex1 + 1, commaIndex2).toInt();
    b = colorData.substring(commaIndex2 + 1).toInt();
}

void RGBLED ::ResetColor(String color)
{

    uint8_t r, g, b;
    extractRGB(color, r, g, b);

    for (uint8_t i = 0; i < RL_NUMS; i++)
    {
        LR_Strip.setPixelColor(i, g, r, b);
        LR_Strip.show();
    }
    for (uint8_t k = 0; k < FB_NUMS; k++)
    {
        FB_Strip.setPixelColor(k, r, g, b);
        FB_Strip.show();
    }
};
void RGBLED ::TurnOff()
{
    for (uint8_t i = 0; i < RL_NUMS; i++)
    {
        LR_Strip.setPixelColor(i, 0, 0, 0);
        LR_Strip.show();
    }

    for (uint8_t k = 0; k < FB_NUMS; k++)
    {
        FB_Strip.setPixelColor(k, 0, 0, 0);
        FB_Strip.show();
    }
};

RGBLED rgb;